package com.example.demo;

import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryCustomizer;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
import org.springframework.stereotype.Component;
import reactor.netty.resources.LoopResources;

import static io.netty.channel.ChannelOption.CONNECT_TIMEOUT_MILLIS;

@Component
public class ServerCustomization extends ReactiveWebServerFactoryCustomizer {
    public ServerCustomization(ServerProperties serverProperties) {
        super(serverProperties);
    }

    @Override
    public void customize(ConfigurableReactiveWebServerFactory factory) {
        super.customize(factory);
        NettyReactiveWebServerFactory nettyFactory = (NettyReactiveWebServerFactory) factory;
        nettyFactory.setResourceFactory(null);
        nettyFactory.addServerCustomizers(server ->
                server.tcpConfiguration(tcpServer ->
                        tcpServer.runOn(LoopResources.create("server", 4, 12, true))
                                .selectorOption(CONNECT_TIMEOUT_MILLIS, 10000)
                )
        );
    }

}
